﻿using GCC.Container.Abstracts;
using GCC.Container.LogProviders;
using GCC.Container.Models;

using Microsoft.AspNetCore.Mvc;

namespace GCC.APIContainer.Utilities
{
    /// <summary>
    /// 容器API抽象基类
    /// </summary>
    [GCCAPIServer]
    public abstract class GCCAPIControllerBase : ControllerBase
    {
        /// <summary>
        /// GCC容器上下文
        /// </summary>
        protected AbsGCCContext GCCContext { get; private set; }

        /// <summary>
        /// 当前控制器的日志
        /// </summary>
        protected AbsGCCLogger Logger { get; init; }


        /// <summary>
        /// 基础的构造函数
        /// </summary> 
        /// <param name="context"></param>
        public GCCAPIControllerBase(AbsGCCContext context)
        {
            this.GCCContext = context;
            this.Logger = GCCLoggerProvider.Instance!.CreateLogger(this.GetType().FullName ?? "Error type");
        }



        /// <summary>
        /// 无效参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected DataResponse<T> InvalidParameter<T>(string? msg = null)
        {

#if DEBUG
            msg += $" eventID:{GCCContext.EventID}";
#endif
            return new DataResponse<T>()
            {
                Code = ResponseCodeEnum.Error | ResponseCodeEnum.Parameter,
                Data = default,
                Msg = msg
            };
        }

        /// <summary>
        /// 系统内部错误
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected DataResponse<T> SystemError<T>(string? msg = null)
        {
#if DEBUG
            msg += $" eventID:{GCCContext.EventID}";
#endif
            return new DataResponse<T>()
            {
                Code = ResponseCodeEnum.Error | ResponseCodeEnum.System,
                Data = default,
                Msg = msg
            };
        }

        /// <summary>
        /// 非法请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected DataResponse<T> IllegalRequest<T>(string? msg = null)
        {
#if DEBUG
            msg += $" eventID:{GCCContext.EventID}";
#endif
            return new DataResponse<T>()
            {
                Code = ResponseCodeEnum.Error | ResponseCodeEnum.Logic,
                Data = default,
                Msg = msg
            };
        }

        /// <summary>
        /// 正常数据处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected DataResponse<T> HandleData<T>(T data, string? msg = null)
        {
#if DEBUG
            msg += $" eventID:{GCCContext.EventID}";
#endif
            return new DataResponse<T>()
            {
                Code = ResponseCodeEnum.Success,
                Data = data,
                Msg = msg
            };
        }

        /// <summary>
        /// 处理带分页的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="count"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected DataResponse<T> HandleData<T>(T data, long count, string? msg = null)
        {
#if DEBUG
            msg += $" eventID:{GCCContext.EventID}";
#endif
            return new PageDataResponse<T>()
            {
                Code = ResponseCodeEnum.Success,
                Data = data,
                DataCount = (int)count,
                Msg = msg,
            };
        }

        /// <summary>
        /// 获取当前登录的用户信息,如果为空,则说明当前调用的接口没有权限认证
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public async Task<GCCUserInfo?> GetCCUserInfo()
        {
            if (!HttpContext.Request.Headers.TryGetValue(GCCContext.GetAuthHeaderKey(), out var token))
            {
                return null;
            }
            var userInfo = await GCCContext.TryGet<GCCUserInfo>(token);
            if (userInfo is null)
            {
                userInfo = await GCCContext.GetUserByToken(token);
            }
            return userInfo;
        }

    }
}